﻿using System;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace OF.Common
{
    /// <summary>
    /// 显示消息提示对话框。
    /// </summary>
    public class MessageBox
    {

        #region alert弹出框

        /// <summary>
        /// 显示消息提示对话框
        /// </summary>
        /// <param name="page">当前页面指针，一般为this</param>
        /// <param name="msg">提示信息</param>
        public static void Show(System.Web.UI.Page page, string msg)
        {
            page.ClientScript.RegisterStartupScript(page.GetType(), "Show1", "<script language='javascript' defer>alert('" + msg.ToString() + "');</script>");
        }

        /// <summary>
        /// 显示消息提示对话框
        /// </summary>
        /// <param name="page">当前页面指针，一般为this</param>
        /// <param name="msg">提示信息</param>
        /// <param name="isAjax">当前页面是否使用了ajax框架;true-使用，false-未使用</param>
        public static void Show(System.Web.UI.Page page, string msg, bool isAjax)
        {
            string strJs = "alert('" + msg + "');";

            if (isAjax)
            {
                MasterPage master = (MasterPage)page.Master;
                if (master != null)
                    ScriptManager.RegisterStartupScript(master.FindControl("ContentPlaceHolder1").FindControl("UpdatePanel1"), page.GetType(), "Show2", strJs, true);
                else
                    ScriptManager.RegisterStartupScript(page.FindControl("UpdatePanel1"), page.GetType(), "Show2", strJs, true);
            }
            else
            {
                page.ClientScript.RegisterStartupScript(
                   page.GetType(), "Show2", "<script language='javascript' defer>" + strJs + "</script>");
            }
        }

        /// <summary>
        /// 带图标的消息提示对话框
        /// </summary>
        /// <param name="page">一般为this</param>
        /// <param name="msg">提示信息</param>
        /// <param name="icon">提示的图标
        /// </param>
        /// <param name="isAjax">当前页面是否使用了ajax框架;true-使用，false-未使用</param>
        public static void Show(System.Web.UI.Page page, string msg, IconBox icon, bool isAjax)
        {
            string strJs = "messageBox('" + msg + "','" + icon + "');";

            if (isAjax)
            {
                MasterPage master = (MasterPage)page.Master;
                if (master != null)
                    ScriptManager.RegisterStartupScript(
                        master.FindControl("ContentPlaceHolder1").FindControl("UpdatePanel1"), page.GetType(), "Show3",
                        strJs, true);
                else
                    ScriptManager.RegisterStartupScript(page.FindControl("UpdatePanel1"), page.GetType(), "Show3", strJs,
                                                        true);
            }
            else
            {
                page.ClientScript.RegisterStartupScript(
                   page.GetType(), "Show3", "<script language='javascript' defer>" + strJs + "</script>");
            }
        }

        #endregion

        #region 弹出提示后跳转页面

        /// <summary>
        /// 显示消息提示对话框，并进行页面跳转
        /// </summary>
        /// <param name="page">当前页面指针，一般为this</param>
        /// <param name="msg">提示信息</param>
        /// <param name="url">跳转的目标URL</param>
        public static void ShowAndRedirect(System.Web.UI.Page page, string msg, string url)
        {
            page.ClientScript.RegisterStartupScript(page.GetType(), "ShowAndRedirect1", "<script language='javascript' defer>alert('" + msg + "');window.location=\"" + url + "\"</script>");
        }

        /// <summary>
        /// 显示带图标的消息提示对话框，并进行页面跳转
        /// </summary>
        /// <param name="page">当前页面指针，一般为this</param>
        /// <param name="msg">提示信息</param>
        /// <param name="url">跳转的目标URL</param>
        /// <param name="icon">提示的图标
        /// cw-错误,sx-伤心,wx-微笑,yw-疑问,zq-正确,jg-警告
        /// </param>
        /// <param name="isAjax">当前页面是否使用了ajax框架;true-使用，false-未使用</param>
        public static void ShowAndRedirect(System.Web.UI.Page page, string msg, string url, IconBox icon, bool isAjax)
        {
            string strJs = "messageBox('" + msg + "','" + icon + "');";

            if (isAjax)
            {
                MasterPage master = (MasterPage)page.Master;
                if (master != null)
                    ScriptManager.RegisterStartupScript(master.FindControl("ContentPlaceHolder1").FindControl("UpdatePanel1"), page.GetType(), "ShowAndRedirect2", strJs + "setTimeout(function (){location='" + url + "'},2500)", true);
                else
                    ScriptManager.RegisterStartupScript(page.FindControl("UpdatePanel1"), page.GetType(), "ShowAndRedirect2", strJs + "setTimeout(function (){location='" + url + "'},2500)", true);
            }
            else
            {
                MessageBox.ShowAndRedirect(page, msg, url);
            }
        }

        /// <summary>
        /// 显示消息提示对话框，并进行页面跳转
        /// </summary>
        /// <param name="page">当前页面指针，一般为this</param>
        /// <param name="msg">提示信息</param>
        /// <param name="url">跳转的目标URL</param>
        public static void ShowAndRedirects(System.Web.UI.Page page, string msg, string url)
        {
            StringBuilder Builder = new StringBuilder();
            Builder.Append("<script language='javascript' defer>");
            Builder.AppendFormat("alert('{0}');", msg);
            Builder.AppendFormat("top.location.href='{0}'", url);
            Builder.Append("</script>");
            page.ClientScript.RegisterStartupScript(page.GetType(), "ShowAndRedirects", Builder.ToString());
        }

        #endregion

        #region 控件点击 消息确认提示框

        /// <summary>
        /// 控件点击 消息确认提示框
        /// </summary>
        /// <param name="page">当前页面指针，一般为this</param>
        /// <param name="msg">提示信息</param>
        public static void ShowConfirm(System.Web.UI.WebControls.WebControl Control, string msg)
        {
            Control.Attributes.Add("onclick", "return confirm('" + msg + "');");
        }

        #endregion

        #region 输出自定义脚本信息

        /// <summary>
        /// 输出自定义脚本信息
        /// </summary>
        /// <param name="page">当前页面指针，一般为this</param>
        /// <param name="script">输出脚本</param>
        /// <param name="isAjax"></param>
        public static void ResponseScript(System.Web.UI.Page page, string script, bool isAjax = false)
        {
            if (isAjax)
            {
                ScriptManager.RegisterStartupScript(page.FindControl("UpdatePanel1"), page.GetType(), "ResponseScript1", script, true);
            }
            else
            {
                page.ClientScript.RegisterStartupScript(
                    page.GetType(), "ResponseScript1", "<script language='javascript' defer>" + script + "</script>");
            }
        }


        /// <summary>
        /// 先弹出提示,后执行JS方法
        /// </summary>
        /// <param name="page"></param>
        /// <param name="msg">提示信息</param>
        /// <param name="fun">JS方法</param>
        /// <param name="isAjax">当前页面是否使用了ajax框架;true-使用，false-未使用</param>
        public static void ShowFun(System.Web.UI.Page page, string msg, string fun, bool isAjax = false)
        {
            string strJs = "alert('" + msg + "');";

            if (isAjax)
            {
                MasterPage master = (MasterPage)page.Master;
                if (master != null)
                    ScriptManager.RegisterStartupScript(master.FindControl("ContentPlaceHolder1").FindControl("UpdatePanel1"), page.GetType(), "ShowFun1", strJs + fun, true);
                else
                    ScriptManager.RegisterStartupScript(page.FindControl("UpdatePanel1"), page.GetType(), "ShowFun1", strJs + fun, true);
            }
            else
                MessageBox.ResponseScript(page, strJs + fun);
        }

        /// <summary>
        /// 消息提示,并进行页面js脚本写入
        /// </summary>
        /// <param name="page">一般为this</param>
        /// <param name="msg">提示信息</param>
        /// <param name="fun">js方法</param>
        /// <param name="icon">提示的图标
        /// cw-错误,sx-伤心,wx-微笑,yw-疑问,zq-正确,jg-警告
        /// </param>
        /// <param name="isAjax">当前页面是否使用了ajax框架;true-使用，false-未使用</param>
        public static void ShowFun(System.Web.UI.Page page, string msg, string fun, string icon, bool isAjax)
        {
            string strJs = "messageBox('" + msg + "','" + icon + "');";

            if (isAjax)
                ScriptManager.RegisterStartupScript(page.FindControl("UpdatePanel1"), page.GetType(), "ShowFun2", strJs + fun, true);
            else
                MessageBox.ResponseScript(page, strJs + fun);
        }


        /// <summary>
        /// 执行js方法
        /// </summary>
        /// <param name="page"></param>
        /// <param name="fun">JS方法</param>
        /// <param name="isAjax">当前页面是否使用了ajax框架;true-使用，false-未使用</param>
        public static void ShowFun(System.Web.UI.Page page, string fun, bool isAjax)
        {
            if (isAjax)
            {
                MasterPage master = (MasterPage)page.Master;
                if (master != null)
                    ScriptManager.RegisterStartupScript(master.FindControl("ContentPlaceHolder1").FindControl("UpdatePanel1"), page.GetType(), "ShowFun3", fun, true);
                else
                    ScriptManager.RegisterStartupScript(page.FindControl("UpdatePanel1"), page.GetType(), "ShowFun3", fun, true);
            }
            else
                MessageBox.ResponseScript(page, fun);
        }

        #endregion

        #region 打开Dialog(必须是Ajax,height=0时则不设弹框高度)

        public static void OpenDialog(string url, string title, int width, int height, System.Web.UI.Page page, UpdatePanel UpdatePanel1)
        {
            if (height == 0)
            {
                ScriptManager.RegisterStartupScript(UpdatePanel1, page.GetType(), "OpenDialog1", "dialogShow('" + url + "','" + title + "'," + width + ");", true);
            }
            else
            {
                ScriptManager.RegisterStartupScript(UpdatePanel1, page.GetType(), "OpenDialog1", "dialogShow('" + url + "','" + title + "'," + width + "," + height + ");", true);
            }
        }
        #endregion

        #region 后台的操作提示说明

        /// <summary>
        /// 操作结果
        /// </summary>
        public enum Result
        {
            /// <summary>
            /// 操作成功
            /// </summary>
            OK,
            /// <summary>
            /// 操作失败
            /// </summary>
            Fail,
            /// <summary>
            /// 警告
            /// </summary>
            Warn,
            /// <summary>
            /// 没有找到数据
            /// </summary>
            NoFind

        }

        /// <summary>
        /// 后台操作结果的说明
        /// </summary>
        /// <param name="panel">放置操作结果的层</param>
        /// <param name="lbl">用于显示操作说明的Label</param>
        /// <param name="result">操作结果的类型，枚举</param>
        /// <param name="msg">要显示的操作结果文字</param>
        public static void ResultMsg(Panel panel, Label lbl, Result result, string msg)
        {
            panel.Visible = true;
            lbl.Text = msg;
            switch (result)
            {
                case Result.OK:
                    panel.CssClass = "message-success";
                    lbl.CssClass = "msg-success";
                    break;
                case Result.Fail:
                    panel.CssClass = "message-error";
                    lbl.CssClass = "msg-error";
                    break;
                case Result.Warn:
                    panel.CssClass = "message-warning";
                    lbl.CssClass = "msg-warning";
                    break;
                case Result.NoFind:
                    panel.CssClass = "message-info";
                    lbl.CssClass = "msg-info";
                    lbl.Text = "抱歉，查无信息！";
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 操作结果自动消失
        /// 页面必须要引用jquery.js和dialog.js
        /// </summary>
        /// <param name="panel">放置操作结果的层</param>
        /// <param name="lbl">用于显示操作说明的Label</param>
        /// <param name="result">操作结果的类型，枚举</param>
        /// <param name="msg">要显示的操作结果文字</param>
        /// <param name="page">传入this</param>
        /// <param name="isAjax">页面是否使用的ajax框架</param>
        public static void ResultMsg(Panel panel, Label lbl, Result result, string msg, Page page, bool isAjax)
        {
            ResultMsg(panel, lbl, result, msg);

            string js = "setTimeout(function(){$('#" + panel.ClientID + "').toggle(500);},3000);";
            ShowFun(page, js, isAjax);
        }


        /// <summary>
        /// 操作成功执行，3秒后关闭弹出框，刷新父窗口
        /// </summary>
        /// <param name="panel">放置操作结果的层</param>
        /// <param name="lbl">用于显示操作说明的Label</param>
        /// <param name="result">操作结果的类型，枚举</param>
        /// <param name="msg">要显示的操作结果文字</param>
        /// <param name="page">传入this</param>
        /// <param name="url">要刷新的父页面url，要包含文件夹名称</param>
        /// <param name="isAjax">页面是否使用的ajax框架</param>
        public static void ResultMsgClose(Panel panel, Label lbl, Result result, string msg, Page page, string url, bool isAjax)
        {
            ResultMsg(panel, lbl, result, msg);

            // 延迟执行弹出框关闭方法
            if (isAjax)
            {
                MasterPage master = (MasterPage)page.Master;
                if (master != null)
                    ScriptManager.RegisterStartupScript(master.FindControl("ContentPlaceHolder1").FindControl("UpdatePanel1"), page.GetType(), "ResultMsgClose", "setTimeOutCloseFrame('" + url + "')", true);
                else
                    ScriptManager.RegisterStartupScript(page.FindControl("UpdatePanel1"), page.GetType(), "ResultMsgClose", "setTimeOutCloseFrame('" + url + "')", true);
            }
            else
                MessageBox.ResponseScript(page, "setTimeOutCloseFrame('" + url + "')");
        }
        /// <summary>
        /// 关闭弹出页面，本方法只限在弹出页面使用。
        /// </summary>
        /// <param name="page">事件来源对象，一般为this</param>
        /// <param name="message">关闭页面前要显示的提示信息，满足string.IsNullOrEmptySpace(massage)时不显示提示信息</param>
        /// <param name="isAjax">页面是否使用的ajax框架</param>
        public static void CloseDialog(Page page, string message = null, bool isAjax = false)
        {
            ShowFun(page, "isClose('" + message + "')", isAjax);
        }

        /// <summary>
        /// 关闭弹出页面并刷新父页面，本方法只限在弹出页面使用。
        /// </summary>
        /// <param name="page">事件来源对象，一般为this</param>
        /// <param name="url">要刷新的页面</param>
        /// <param name="message">关闭页面前要显示的提示信息，满足string.IsNullOrEmptySpace(massage)时不显示提示信息</param>
        /// <param name="isAjax">页面是否使用的ajax框架</param>
        public static void RefreshParent(Page page, string url, string message = null, bool isAjax = false)
        {
            ShowFun(page, "f5Iframe('" + message + "','" + url + "')", isAjax);
        }

        #endregion

        #region 跳转页面
        /// <summary>
        /// 登录超时，跳转到登录页
        /// </summary>
        /// <param name="page"></param>
        public static void AdminRedirectLoginTimeOut(System.Web.UI.Page page)
        {
            page.Response.Write("<script>alert('登录超时,请重新登录!');window.top.location.href='/admin/login.aspx';</script>");
        }

        /// <summary>
        /// 没有权限，跳转到指定页面
        /// </summary>
        /// <param name="page"></param>
        /// <param name="url"></param>
        public static void AdminRedirectUrl(System.Web.UI.Page page, string url)
        {
            if(string.IsNullOrWhiteSpace(url))
                page.Response.Write("<script>alert('对不起，您没有操作权限!'); history.go(-1);</script>");
            else
                page.Response.Write("<script>window.top.location.href='/" + url + "';</script>");
        }
        #endregion

    }

    #region 弹出框提示图标

    public enum IconBox
    {
        /// <summary>
        /// 成功
        /// </summary>
        Succeed = 0,
        /// <summary>
        /// 失败
        /// </summary>
        Error,
        /// <summary>
        /// 警告
        /// </summary>
        Warning,
        /// <summary>
        /// 伤心
        /// </summary>
        Sad,
        /// <summary>
        /// 笑
        /// </summary>
        Smile,
        /// <summary>
        /// 疑问
        /// </summary>
        Question,
    }

    #endregion
}
